package com.homework.util;

import lombok.Data;
import lombok.NoArgsConstructor;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

import java.io.Serializable;

@Data
@NoArgsConstructor
public class ResultMsg<T, K> implements Serializable {
    //@JsonInclude(Include.NON_NULL)
    private T data;

    @JsonInclude(Include.NON_NULL)
    private K error;
    private String msg = "成功";
    private int code = 0;

    public void buildCode(ErrorCode errorCode) {
        this.code = errorCode.getCode();
        this.msg = errorCode.getMsg();
    }

    public enum ErrorCode {
        OK(0, "成功"),
        Param(1, "参数错误"),
        Authen(2, "认证错误"),
        NoData(3, "数据集为空"),
        Server(4, "服务器内部错误"),
        Connect(99, "连接错误");

        private int code;
        private String msg;

        ErrorCode(int code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        public static ErrorCode getByCode(int index) {
            for (ErrorCode code : ErrorCode.values()) {
                if (code.code == index) {
                    return code;
                }
            }
            return null;
        }

        public int getCode() {
            return code;
        }

        public String getMsg() {
            return msg;
        }
    }
}
